"use strict";
var page = require('webpage').create(),
    system = require('system'),
    start, address;
// window.console.log(msg);
page.onConsoleMessage = function (msg) {
    console.log(msg);
    printArgs.apply(this, arguments);
};
if (system.args.length === 1) {
    console.log('Usage: loadspeed.js <some URL>');
    phantom.exit(1);
} else {
    start = Date.now();
    address = system.args[1];
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('FAIL to load the address');
        } else {
            var t = Date.now() - start;
            console.log('Loading time ' + t / 1000 + ' sec');
            var ua = page.evaluate(function () {
                return document.title;
            });
            console.log(ua);


            page.evaluate(function () {
                /*模拟窗口滚动*/
                var simulateScroll = function (simulateScrollCallback) {
                    console.log("start to caculate height")
                    var body = document.body,
                        html = document.documentElement;
                    var height = Math.max(body.scrollHeight, body.offsetHeight,
                        html.clientHeight, html.scrollHeight, html.offsetHeight);
                    console.log("height is " + height);

                    var scrollAndWait = function (h, height) {
                        console.log("start to scrollAndWait")
                        console.log("scroll from " + h)
                        h = h + window.innerHeight;
                        console.log("scroll to " + h);
                        window.scroll(0, h);
                        if (h < height) {
                            var start = Date.now();
                            for (; (Date.now() - start) < 2000;);
                            scrollAndWait(h, height);
                        }
                        else if (typeof simulateScrollCallback === "function")
                            simulateScrollCallback();
                    };
                    scrollAndWait(0, height);
                };
                simulateScroll(function () {
                    var start = Date.now();
                    var images = document.getElementsByTagName("img");
                    console.log("images size is :" + images.length);
                    for (var i = 0; i < images.length; i++) {
                        if (images[i].getAttribute("src") != undefined && images[i].getAttribute("src").indexOf("image/gif;base64") > 0)
                            images[i].setAttribute("src", images[i].getAttribute("data-src"));
                        console.log("Img " + i + ": " + images[i].getAttribute("src"));
                        var t = Date.now() - start;
                        console.log('Loading time ' + t / 1000 + ' sec');
                    }
                })
            });
            /*去掉所有script标签*/
            page.evaluate(function () {
                while(document.getElementsByTagName("script").length>0)
                    document.getElementsByTagName("script")[0].outerHTML="";
            });
            /*获取文章正文*/
            page.evaluate(function () {
                console.log("start download body");
                console.log(document.body.outerHTML);
                console.log("end download body");
            });
        }
        phantom.exit();
    });
}
